package concurrence;

/**
 * Created by gzhd on 2017/11/20.
 */
public class BadLockOnInteger implements Runnable {
    public static Integer i=0;
    static BadLockOnInteger instance = new BadLockOnInteger();
    @Override
    public void run() {
        for (int j=0;j<1000000;j++) {
//            synchronized (i) {
            synchronized (instance) {
                i++;
            }
        }

    }


    public static void main(String[] args)  throws Exception{

        long start=System.currentTimeMillis();
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        Thread t3 = new Thread(instance);
        Thread t4 = new Thread(instance);
        Thread t5 = new Thread(instance);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        t5.join();



        long end=System.currentTimeMillis();
        System.out.println(i);
        System.out.println(end-start);
    }
}
